package com.jrh.web.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.jrh.biz.img.ImgUploadService;
import com.jrh.biz.user.UserService;
import com.jrh.dal.UserDao;
import com.jrh.dal.UserProfileDao;
import com.jrh.domain.entity.User;
import com.jrh.domain.entity.UserProfile;
import com.jrh.domain.user.LoginUserInfo;
import com.jrh.web.member.tools.UserAuthUtil;

/**
 * 类UserController.java的实现描述：用户中心
 * 
 * @author mmyjse@gmail.com 2012-8-5 上午3:14:21
 */
@Controller
@RequestMapping("/settings")
public class SettingsController {

    @Autowired
    private UserService      userService;

    @Autowired
    private UserProfileDao   userProfileDao;

    @Autowired
    private UserDao          userDao;

    @Autowired
    private ImgUploadService imgUploadService;

    public static final int  FACE_WIDTH  = 100;
    public static final int  FACE_HEIGHT = 100;

    @RequestMapping(method = RequestMethod.GET, value = "/index")
    public String list(Model model) {
        return "user/index";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/personal")
    public String personal(Model model, HttpServletRequest request, HttpServletResponse response) {
        LoginUserInfo userInfo = UserAuthUtil.getUserInfo(request, response);
        if (userInfo == null) {
            return "error";
        }
        UserProfile userProfile = userProfileDao.getUserProfileByUserId(userInfo.getId());
        model.addAttribute("userProfile", userProfile);

        User user = userDao.load(userInfo.getId());
        model.addAttribute("user", user);
        return "user/personal";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/personal")
    public String doPersonal(@ModelAttribute("userProfile") UserProfile userProfile, MultipartFile faceImage,
                             Model model, HttpServletRequest request, HttpServletResponse response) {
        LoginUserInfo userInfo = UserAuthUtil.getUserInfo(request, response);
        if (userInfo == null) {
            return "error";
        }
        userProfile.setUserId(userInfo.getUserId());

        String faceUrl = imgUploadService.upload(faceImage, "face");
        faceUrl = imgUploadService.resize(faceUrl, FACE_WIDTH, FACE_HEIGHT);
        boolean success = userService.updateUserProfile(userProfile);
        if (success) {
            model.addAttribute("success", Boolean.TRUE);

            User user = userDao.load(userInfo.getId());
            model.addAttribute("user", user);
            return "user/personal";
        } else {
            model.addAttribute("success", Boolean.FALSE);
            model.addAttribute("errorMsg", "修改用户个人信息错误");
            return "error";
        }
    }
}
